package cn.com.controller;

import cn.com.beans.BatchLine;
import cn.com.service.BatchLineService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/insertBatchLine")
public class InsertBatchLineController {
    @Resource
    private BatchLineService batchLineService;

    @GetMapping
    public String home(){
        return "Insert/InsertBatchLine";
    }

    @PostMapping("/insert")
    @ResponseBody
    public Map<String,Object> insert(HttpServletRequest request){
        Map<String,Object> result = new HashMap<>();
        Integer year = Integer.valueOf(request.getParameter("year"));
        String province = request.getParameter("province");
        Integer frType = Integer.valueOf(request.getParameter("frType"));
        Integer baOne = Integer.valueOf(request.getParameter("baOne"));
        Integer baTwo = Integer.valueOf(request.getParameter("baTwo"));
        Integer baThree = Integer.valueOf(request.getParameter("baThree"));
        String type;
        if(frType == 0){
            type = "文科";
        }else{
            type = "理科";
        }
        BatchLine batchLine = new BatchLine();
        batchLine.setBaYear(year);
        batchLine.setBaProvince(province);
        batchLine.setBaType(type);
        batchLine.setBaOne(baOne);
        batchLine.setBaTwo(baTwo);
        batchLine.setBaThree(baThree);
        BatchLine batch = batchLineService.findBatch(batchLine);
        if( batch != null){
            result.put("code",0);
            result.put("msg","已经存在添加失败");
            return result;
        }
        int i = batchLineService.insert(batchLine);
        if(i == 1){
            result.put("code",1);
            result.put("msg","添加成功");
            return result;
        }else{
            result.put("code",2);
            result.put("msg","添加失败");
            return result;
        }
    }
}
